package com.flysky.service.finance.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.flysky.common.ToolUtil;
import com.flysky.dao.finance.IFinanceDao;
import com.flysky.model.finance.BigTypeHbmModel;
import com.flysky.model.finance.FinanceHbmModel;
import com.flysky.model.finance.SmallTypeHbmModel;
import com.flysky.service.finance.IFinanceService;

public class FinanceServiceImpl implements IFinanceService {
	
	IFinanceDao financeDao = null;
	
	public void setFinanceDao(IFinanceDao financeDao) {
		this.financeDao = financeDao;
	}

	@Override
	public List<BigTypeHbmModel> getAllBigType() {
		List<BigTypeHbmModel> bigTypeList = this.financeDao.getAllFinanceBigType();
		//remove the repeated data in the list.
		for(int i = 0; i < bigTypeList.size(); i++){
			for(int j = bigTypeList.size()-1; j > i; j--){
				if(bigTypeList.get(i).equals(bigTypeList.get(j))){
					bigTypeList.remove(j);
				}
			}
		}
		return bigTypeList;
	}

	@Override
	public void addbigType(String typeName) {
		BigTypeHbmModel newBigType = new BigTypeHbmModel();
		newBigType.setType_name(typeName);
		this.financeDao.addFinanceBigType(newBigType);
	}

	@Override
	public void deleteBigType(String typeId) {
		int type_id = Integer.parseInt(typeId.replace("bigType", ""));
		BigTypeHbmModel bigType = this.financeDao.getFinanceBigType(type_id);
		this.financeDao.deleteFinanceBigType(bigType);
	}

	@Override
	public void addSmallType(String typeName, String bigTypeId) {
		int bigType_id = Integer.parseInt(bigTypeId);
		BigTypeHbmModel bigType = this.financeDao.getFinanceBigType(bigType_id);
		SmallTypeHbmModel smallType = new SmallTypeHbmModel();
		smallType.setType_name(typeName);
		smallType.setBigType(bigType);
		this.financeDao.addFinanceSmallType(smallType);
	}

	@Override
	public void deleteSmallType(String typeId) {
		int type_id = Integer.parseInt(typeId.replace("smallType", ""));
		SmallTypeHbmModel smallType = this.financeDao.getFinanceSmallType(type_id);
		this.financeDao.deleteFianceSmallType(smallType);
	}

	@Override
	public void addNewFinance(FinanceHbmModel financeModel,String smallTypeId) {
		Date currentDate = new Date();
		financeModel.setCreateDate(currentDate);
		int type_id = Integer.parseInt(smallTypeId);
		SmallTypeHbmModel smallType = this.financeDao.getFinanceSmallType(type_id);
		financeModel.setSmallType(smallType);
		this.financeDao.addFinance(financeModel);
	}

	@Override
	public List<FinanceHbmModel> getAllFinanceItems(
			HashMap<String, String> paramMap) {
		//get month information if null set current month default.
		String year = paramMap.get("YEAR");
		String month = paramMap.get("MONTH");
		if(ToolUtil.isEmpty(year)){
			Calendar date = Calendar.getInstance();
			year = String.valueOf(date.get(Calendar.YEAR));
			paramMap.put("YEAR", year);
		}
		if(ToolUtil.isEmpty(month)){
			Calendar date = Calendar.getInstance();
			month = String.valueOf(date.get(Calendar.MONTH));
			paramMap.put("MONTH", month);
		}
		List<FinanceHbmModel> resultList = this.financeDao.getAllMatchedFinances(paramMap);
		return resultList;
	}

	@Override
	public Long getMonthFinanceCounts(HashMap<String, String> paramMap) {
		return this.financeDao.countAllMatchFinances(paramMap);
	}

	@Override
	public Map<String,String> viewFinanceInfo(
			HashMap<String, String> paramMap) {
		Map<String,String> statistics = new HashMap<String,String>();
		List<FinanceHbmModel> resultList = getAllFinanceItems(paramMap);
		double incomeTotal = 0.0;
		double spendTotal = 0.0;
		double leftTotal = 0.0;
		for(FinanceHbmModel item : resultList){
			if(item.getSmallType().getBigType().getType_id()==7){
				incomeTotal += item.getMoney();
			}else{
				spendTotal += item.getMoney();
			}
		}
		leftTotal = incomeTotal - spendTotal;
		statistics.put("incomeTotal", String.valueOf(incomeTotal));
		statistics.put("spendTotal", String.valueOf(spendTotal));
		statistics.put("leftTotal", String.valueOf(leftTotal));
		return statistics;
	}

}
